-- 每个entity在创建时，都会新建一个entity_properties组件，以key-value的形式存储entity相关的所有属性值

-- 模块定义
local M = 
{
    _base_class = require "components.BaseComponent",
    _class_name = "components.EntityProperties"
};
setmetatable (M, {__index = _G;});
_ENV = M;

-- 引用模块
local Tools = require "tools.CommonTools"
local Map = require "collections.Map"

-- 方法定义
operation_table = Tools.CreateOperationTable (_base_class);

-- 添加属性值，如果已存在，则抛出错误
function operation_table.AddProperty (self, propertyName, propertyValue)
    self._properties:Add (propertyName, propertyValue);
end;

-- 设置属性值，如果不存在，则抛出错误
function operation_table.SetProperty (self, propertyName, propertyValue)
    self._properties:SetItem (propertyName, propertyValue);
end;

-- 获取属性值，如果不存在，则返回指定的默认值
function operation_table.GetProperty (self, propertyName, defaultValue)
    local result = self._properties:GetItem (propertyName);
    if (result == nil) then
        return defaultValue;
    else
        return result;
    end;
end;

-- 删除属性值，如果不存在，则什么都不做
function operation_table.RmvProperty (self, propertyName)
    self._properties:Rmv (propertyName);
end;

-- 构造函数 
function NewEntityProperties ()
    local instance = _base_class.NewBaseComponent ();
    Tools.PrepareInstance (M, instance);
    
    instance._properties = Map.NewMap ();

    return instance;
end;

return M;
